// controllers/articleController.js
const Article = require('../models/article');
const articleController = {
  createArticle: (req, res) => {
    const { title, user_id, username, img, content } = req.body;
    if (!title || !user_id || !username || !img || !content) {
      return res.status(200).send({
        message: '字段缺少',
        success: false,
      });
    }
    // 状态默认为 'pending'，不需要客户端提供
    const status = 'pending';
    Article.create({ title, content, status, user_id, username, img }, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else {
        res.status(200).send({
          message: '操作成功',
          data: {
            id: results.insertId,
          },
          success: true,
        });
      }
    });
  },
  getArticleById: (req, res) => {
    const { id } = req.body; // 通过 req.body 获取 id
    if (!id) {
      return res
        .status(200)
        .send({ message: 'Article ID is required', success: false });
    }
    Article.findById(id, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else if (!results.length) {
        res.status(200).send({ message: 'Article not found', success: false });
      } else {
        res.status(200).send({ data: results[0], success: true });
      }
    });
  },
  getAllArticles: (req, res) => {
    const page = parseInt(req.body.page, 10) || 1;
    const limit = parseInt(req.body.limit, 10) || 10;
    Article.findAll(page, limit, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else {
        Article.find((error, words) => {
          res.status(200).send({ data: results, page, limit, total: words.length, success: true });
        })
      }
    });
  },
  updateArticle: (req, res) => {
    const { id, title, content, status } = req.body;
    if (!id || !title || !content || !status) {
      return res.status(200).send({
        message: '参数不对',
        success: false,
      });
    }
    Article.update(id, { title, content, status }, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else if (results.affectedRows === 0) {
        res.status(200).send({
          message: '数据不存在',
          success: false,
        });
      } else {
        res
          .status(200)
          .send({ message: '操作成功', success: true });
      }
    });
  },
  deleteArticle: (req, res) => {
    const { id } = req.body;
    if (!id) {
      return res
        .status(200)
        .send({ message: 'Article ID is required', success: false });
    }
    Article.delete(id, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else if (results.affectedRows === 0) {
        res.status(200).send({ message: 'Article not found', success: false });
      } else {
        res
          .status(200)
          .send({ message: 'Article deleted successfully', success: true });
      }
    });
  },
  // 其他控制器方法...
};

module.exports = articleController;